<template>
  <div class="resource-category">
    <el-card>
      <!-- 头部结构 -->
      <div class="hedader">
        <el-form>
        <el-form-item>
        <el-button
          @click="handleAdd"
          style="margin-right:10px">添加
        </el-button>
        </el-form-item>
        </el-form>
      </div>
      <!-- 添加资源分类的对话框结构 -->
      <el-dialog :title="isEdit ? '编辑资源分类' : '添加分类'"
         :visible.sync="dialogVisible"
         width="30%">
         <!-- 将添加与编辑封装到组建中 -->
        <categories-create-or-edit
        :resourcecategories="resourcecategories"
        :is-edit="isEdit"
        :category-id="categoryId"
        @success="successAdd"
        @cancel="cancelAdd"></categories-create-or-edit>
      </el-dialog>
      <!-- 主体结构 -->
      <el-table
        :data="resourcecategories"
        style="width: 100%"
         v-loading="isLoading">
        <el-table-column
          prop="id"
          label="编号"
          width="180">
        </el-table-column>
        <el-table-column
          prop="name"
          label="名称">
        </el-table-column>
        <!-- 如果要在当前的功能需要设置过滤器那么得获取到数据，
        获取数据就得要用自定义列模板 的作用域插槽工具 -->
        <el-table-column
          label="创建时间">
          <template slot-scope="scope">
            <span>{{ scope.row.createdTime | dateFormat }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="sort"
          label="排序">
        </el-table-column>
        <el-table-column
          prop="address"
          label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.row)">
              编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)">
              删除
            </el-button>
          </template>
         </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import CategoriesCreateOrEdit from './components/CategoriesCreateOrEdit'
import { getResourcecategories, deleteResourceCategory } from '@/services/resource'
export default {
  name: 'ResourceCategory',
  components: {
    CategoriesCreateOrEdit
  },
  data () {
    return {
      // 控制对话框的展示
      dialogVisible: false,
      // 控制新增对话框的功能状态(编辑还是添加)
      isEdit: false,
      // 存储资源分类信息
      resourcecategories: [],
      // 存储编辑时的资源分类的id
      categoryId: ''
    }
  },
  created () {
    // 加载资源分类列表
    this.loadgetResourcecategories()
  },
  methods: {
    async loadgetResourcecategories () {
      const { data } = await getResourcecategories()
      console.log(data)
      if (data.code === '000000') {
        this.resourcecategories = data.data
      }
    },
    handleDelete (rowData) {
      // console.log(rowData.id)
      // 删除的确认提示
      this.$confirm('确认删除吗?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 进行删除操作发送请求到服务器端进行删除
        // console.log(rowData.id)
        const { data } = await deleteResourceCategory(rowData.id)
        // console.log(data)
        if (data.code === '000000') {
          // 更新资源分类列表
          this.loadgetResourcecategories()
          // 提示信息
          this.$message({
            type: 'success',
            message: '已删除成功!'
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 点击编辑显示对话框 对话框的功能状态为编辑
    handleEdit (rowData) {
      this.dialogVisible = true
      this.isEdit = true
      this.categoryId = rowData.id
      console.log(this.categoryId)
    },
    successAdd () {
      // 关闭提示框,并更新资源分类列表
      this.dialogVisible = false
      this.loadgetResourcecategories()
    },
    cancelAdd () {
      // 关闭提示框
      this.dialogVisible = false
    },
    // 点击添加 显示对话框 对话框的功能状态为添加
    handleAdd () {
      this.dialogVisible = true
      this.isEdit = false
    }
  },
  filters: {
    dateFormat (date) {
      date = new Date(date)
      return `${date.getFullYear()}-
      ${date.getMonth() + 1}-
      ${date.getDate()}-
      ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
    }
  }
}
</script>

<style lang="scss" scoped></style>
